﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace zy_simu.Data
{
    internal class CommPkg
    {
        private byte[] buf;
        public CommPkg(ref byte[] buf)
        {
            this.buf = buf;
            data = buf[4..];
        }
        public byte flag                // 0xAA flag
        {
            get { return buf[0]; }
            set { buf[0] = value; }
        }
        public ushort len
        {
            get { return (ushort)(buf[1] + ((buf[2] << 8) & 0x0700)); }
            set { buf[1] = (byte)(value & 0xff); buf[2] |= (byte)((value & 0x0700) >> 8); }
        }
        public ushort cmd
        {
            get { return (ushort)((buf[2] & 0xf8) >> 3); }
            set { buf[2] |= (byte)((value << 3) & 0xf8); }
        }
        public byte checksum       // 整包数据含ZqCommPkg结构校验
        {
            get { return buf[3]; }
            set { buf[3] = value; }
        }
        public byte[] data;

        public static byte GetChecksum(byte[] data, uint len)
        {
            byte s = 0;
            for (int i = 0; i < len; i++)
            {
                s ^= data[i];
            }
            return s;
        }
        public static int GetHeadSize()
        {
            return 4;
        }
    }
}
